﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithm_2.Easy_599_FindRestaurant
{
    class MainProject
    {
        static void Main(string[] args)
        {
            foreach (var item in FindRestaurant(new string[] { "Shogun", "Tapioca Express", "Burger King", "KFC" }, new string[] { "Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun" }))
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
        public static string[] FindRestaurant(string[] list1, string[] list2)
        {
            #region HashSet
            List<string> list = new List<string>();

            int minNum = int.MaxValue;

            //HashSet
            HashSet<string> hash = new HashSet<string>();
            foreach (var item in list1) hash.Add(item);

            foreach (var item in list2)
            {
                if (!hash.Add(item))
                {
                    int list1Index = hash.ToList().IndexOf(item);
                    int list2Index = list2.ToList().IndexOf(item);
                    if (list1Index+ list2Index< minNum)
                    {
                        minNum = list1Index + list2Index;
                        list.Clear();
                        list.Add(item);
                    }
                    else if(list1Index + list2Index == minNum) list.Add(item);

                }
            }
            return list.ToArray();
            #endregion
        }

    }
}
